package sis.testio.piped;/*
 * @auther 顶风少年
 * @mail dfsn19970313@foxmail.com
 * @date 2019-07-29 07:54
 * @notify
 * @version 1.0
 */

import junit.framework.TestCase;

import java.io.PipedReader;
import java.io.PipedWriter;

public class testPipedReaderWriter extends TestCase {

    public void testPipedReaderWriter() {
        /**
         * 管道流通信核心是,Writer和Reader公用一块缓冲区,缓冲区在Reader中申请,
         * 由Writer调用和它绑定的Reader的Receive方法进行写.
         *
         * 线程间通过管道流通信的步骤为
         * 1 建立输入输出流
         * 2 绑定输入输出流
         * 3 Writer写
         * 4 Reader读
         */
        PipedReader reader = new PipedReader();
        PipedWriter writer = new PipedWriter();
        Producer producer = new Producer(writer);
        Consumer consumer = new Consumer(reader);
        try {
            writer.connect(reader);
            producer.start();
            consumer.start();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}